using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Trang;
using System.Diagnostics;
using System.Security.AccessControl;

using System.IO;

namespace ArchSoft
{
    public partial class T3VTitleBar : AlphaBlendTextBox
    {
        private T3VForm parentForm = null;
        string _oldCaptionWindow = string.Empty;
        string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
        public T3VForm ParentT3VForm
        {
            set
            {
                parentForm = value;
            }
        }

        public T3VTitleBar()
        {
            InitializeComponent();
            this.mnuDWG.Click += new EventHandler(mnuDWG_Click);
            this.mnuPdf.Click += new EventHandler(mnuPdf_Click);
            this.MnuAttTitle.Click += new EventHandler(MnuAttTitle_Click);
        }

        void MnuAttTitle_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            this.parentForm.AttachTitleBlock(true);
            parentForm.t3VListView1.DisableWatcher(true);
        }

        void mnuDWG_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            IssuedFrm fr = new IssuedFrm(false);
            fr.StartPosition = FormStartPosition.CenterScreen;
            if (fr.ShowDialog() == DialogResult.OK)
                this.parentForm.SendToIssued(true, fr.ListOfFolderNedIss, fr.txtDes.Text);
            parentForm.t3VListView1.DisableWatcher(true);
        }

        void mnuPdf_Click(object sender, EventArgs e)
        {
            string output = this.parentForm.getOutPutFolder();

            string curProject = Directory.GetParent(output).FullName;



            MyFrm fr = new MyFrm(false);

            fr.setCurDir(curProject);

            fr.StartPosition = FormStartPosition.CenterScreen;

            if (fr.ShowDialog() == DialogResult.Yes)
                this.parentForm.SendToIssuedPDF(true, fr.ListOfFolderNedIss, fr.ListOfPageSetup, fr.txtDes.Text);
        }

        //TODO: removed.
        public T3VTitleBar( T3VForm form )
        {
            this.parentForm = form;
            InitializeComponent();
            this.mnuDWG.Click += new EventHandler(mnuDWG_Click);
            this.mnuPdf.Click += new EventHandler(mnuPdf_Click);
            this.mnuEmail.Click +=new EventHandler(mnuEmail_Click);
            this.MnuAttTitle.Click += new EventHandler(MnuAttTitle_Click);
            this.mnuInsertViewport.Click += new EventHandler(mnuInsertViewport_Click);
            this.MnuLayout.Click += new EventHandler(MnuLayout_Click);
            this.mnuLayer.Click += new EventHandler(mnuLayer_Click);
            this.mnuLineType.Click += new EventHandler(mnuLineType_Click);
            this.MnuName.Click += new EventHandler(MnuName_Click);
            this.MnuScale.Click += new EventHandler(MnuScale_Click);
            this.MnuDimm.Click += new EventHandler(MnuDimm_Click);
            SetupContextMenu();
        }

        void mnuEmail_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Email");
            //string[] attachFiles = GetSelection();
            //string output = this.parentForm.getOutPutFolder();

            //string curProject = Directory.GetParent(output).FullName;
            //MailFrm frm = new MailFrm();
            //frm.ProjectPath = curProject;
            //frm.AttachFiles = attachFiles;
            //frm.ShowDialog();

            string output = this.parentForm.getOutPutFolder();

            string curProject = Directory.GetParent(output).FullName;

            string folder =this.parentForm.getOutPutFolder() + Path.DirectorySeparatorChar +  this.parentForm.captionWindow;
            string[] attachFiles = Directory.GetFiles(folder);

            MailFrm frm = new MailFrm();
            frm.setForm(this.parentForm);
            frm.setSendAll = true;
            frm.ProjectPath = curProject;
            frm.AttachFiles = attachFiles;
            frm.ShowDialog();

        }

        void MnuDimm_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            this.parentForm.SynchronizeDimension(true);
            parentForm.t3VListView1.DisableWatcher(true);
        }

        void MnuScale_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            this.parentForm.SynchronizeScale(true);
            parentForm.t3VListView1.DisableWatcher(true);
        }

        void MnuName_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            this.parentForm.SynchronizeDrawingName(true);
            parentForm.t3VListView1.DisableWatcher(true);
        }

        void mnuLineType_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            this.parentForm.SynchronizeLineType(true);
            parentForm.t3VListView1.DisableWatcher(true);
        }

        void mnuLayer_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            this.parentForm.SynchronizeLayer(true);
            parentForm.t3VListView1.DisableWatcher(true);
        }

        void MnuLayout_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            this.parentForm.SynchronizeDrawingLayout(true);
            parentForm.t3VListView1.DisableWatcher(true);
        }

        void mnuInsertViewport_Click(object sender, EventArgs e)
        {
            parentForm.t3VListView1.DisableWatcher(false);
            this.parentForm.InsertViewport(true);
            parentForm.t3VListView1.DisableWatcher(true);
        }
        //override onk

        public void SetupContextMenu()
        {
            if (this.parentForm.IsReferenceWindow)
            {
                mnuPublish.Visible = false;
                mnuDelete.Visible = false;
                mnuDelete.Enabled = false;
                mnuSendToIssue.Visible = false;
                mnuRename.Visible = false;
                mnuRename.Enabled = false;
                MnuAttTitle.Visible = false;
                mnuInsertViewport.Visible= false;
                MnuSyn.Visible = false;
                toolStripSeparator3.Visible = false;
                toolStripSeparator4.Visible = false;
                toolStripSeparator1.Visible = false;
            }            
        }

        public void AddAccessRule(string path)
        {
            DirectoryInfo dInfor = new DirectoryInfo(path);
            DirectorySecurity dSecurity = dInfor.GetAccessControl();
            dSecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Deny));
            dInfor.SetAccessControl(dSecurity);
        }

        public void RemoveAccessRule(string path)
        {
            DirectoryInfo dInfor = new DirectoryInfo(path);
            DirectorySecurity dSecurity = dInfor.GetAccessControl();
            dSecurity.RemoveAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Deny));
            dInfor.SetAccessControl(dSecurity);
        }

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
               // RemoveAccessRule(this.parentForm.OutputFolderPath); 
                if (e.ClickedItem.Name == "mnuNewDrawing")
                {
                    this.parentForm.AddNewDrawing();
                }
                else if (e.ClickedItem.Name == "mnuDelete")
                {
                    contextMenuStrip1.Hide();
                    if (MessageBox.Show(Constants.DELETE_WINDOW_MESSAGE, Constants.PROGRAM_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        this.parentForm.Delete();
                }
                else if (e.ClickedItem.Name == "mnuRename")
                {
                   
                    _oldCaptionWindow = this.parentForm.CaptionWindow;
                    this.Text = CodeNameUtilities.GetWindowNameFromFullName(this.parentForm.CaptionWindow);                    
                    this.ReadOnly = false;
                    this.BackAlpha = 100;
                    this.BackColor = Color.White;
                    this.ForeColor = Color.Black;
                    this.Focus();
                    this.SelectAll();
                }
                else if (e.ClickedItem.Name == "mnuOpenFolder")
                {
                    //MessageBox.Show(this.parentForm.OutputFolderPath);
                   
                   Process.Start(this.parentForm.FullFolderPath);
                  
                }

                //AddAccessRule(this.parentForm.OutputFolderPath);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
        public void SetElementName(string name,string oldname,int NumofDrawing)
        {
            _oldCaptionWindow = oldname;
            this.Text = string.Format("{0} ({1})", name, NumofDrawing);
            this.parentForm.SetTitleName(name);
            this.parentForm.t3VListView1.ReInitializeEventForFolders();
            
        }
        private void T3VTitleBar_Leave(object sender, EventArgs e)
        {
            if (this.ReadOnly)
                return;
            OnEditingFinished();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);            

            if (e.KeyValue == (int)Keys.Enter)
            {
                if (this.ReadOnly)
                    return;
                OnEditingFinished();
            }
            else if (e.KeyValue == (int)Keys.Escape)
            {
                this.ReadOnly = true;
                this.BackAlpha = 0;
                this.BackColor = Color.White;
                this.ForeColor = Color.Black;
                this.parentForm.UpdateTitle();
            }
        }

        private void OnEditingFinished()
        {
            try
            {
                this.parentForm.t3VListView1.DisableWatcher(false);
                ((MDIChildForm)this.parentForm.Parent.Parent).EnableWatcher(false);
                this.parentForm.t3VListView1.ReleaseWatcher();
                this.BackAlpha = 0;
                this.BackColor = Color.White;
                this.ForeColor = Color.Black;
                this.ReadOnly = true;
                if (this.Text != string.Empty)
                    this.parentForm.CaptionWindow =CodeNameUtilities.ReplaceWindowName(_oldCaptionWindow, this.Text);
                else
                    this.parentForm.UpdateTitle();
                this.parentForm.t3VListView1.ReInitializeEventForFolders();
                this.parentForm.t3VListView1.DisableWatcher(true);
                ((MDIChildForm)this.parentForm.Parent.Parent).EnableWatcher(false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ContextMenuOpening(object sender, CancelEventArgs e)
        {
            if (this.parentForm.FullFolderPath == string.Empty)
            {
                this.contextMenuStrip1.Items[0].Visible = false;
                this.contextMenuStrip1.Items[1].Visible = false;
            }
            if (this.parentForm.IsReferenceWindow)                       
                return;
                     

            ArchsoftProcessor.PutLayoutMenuItems(mnuPublish, PublishMenuItemClick, mnuPdf, PDFMenuItemClick);
            if (this.parentForm.FullFolderPath == string.Empty)
            {
                mnuPublish.Enabled = false;
                mnuSendToIssue.Enabled = false;
                mnuInsertViewport.Enabled = false;
                MnuAttTitle.Enabled = false;
                MnuSyn.Enabled = false;
            }
            else
            {
                mnuPublish.Enabled = (mnuPublish.DropDownItems.Count > 0) && this.parentForm.HasFiles;
                mnuSendToIssue.Enabled = true & this.parentForm.HasFiles;
                mnuInsertViewport.Enabled = true & this.parentForm.HasFiles;
                MnuAttTitle.Enabled = true & this.parentForm.HasFiles;
                MnuSyn.Enabled = true & this.parentForm.HasFiles;
            }
        }

        private void PublishMenuItemClick(object sender, EventArgs args)
        {
            if (sender is ToolStripItem)
            {
                ToolStripItem item = (ToolStripItem)sender;
                this.parentForm.PublicDocuments(item.Text, true);
            }
        }

        private void PDFMenuItemClick(object sender, EventArgs args)
        {
            if (sender is ToolStripItem)
            {
                ToolStripItem item = (ToolStripItem)sender;
                this.parentForm.PublicDocuments(item.Text, true);
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (!this.ReadOnly)
            {
                this.OnKeyDown(new KeyEventArgs(keyData));
                return false;
            }
            else
                return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
